import Vue from 'vue'
import Vuex from 'vuex'
import Lockr from 'lockr'
import {
  addAuth,
  removeAuth
} from '@/utils/auth'
import {
  loginAPI,
  logoutAPI,
  sendSMS
} from '@/api/login'
import {
  resetRouter
} from '@/router'


Vue.use(Vuex)


const state = {
  userInfo: null, // 用户信息
  // 权限信息
  allAuth: null, // 总权限信息 默认空 调整动态路由
  socketAction: ''
}

const mutations = {
  SET_USERINFO: (state, userInfo) => {
    state.userInfo = userInfo
    localStorage.setItem('loginUserInfo', JSON.stringify(userInfo));
    Lockr.set('UserInfo', userInfo)
  },
  SET_AUTH: (state, data) => {
    console.info("data:",data);
    const token = data.token
    const sessionId = data.token
    Lockr.set('authToken', token)
    Lockr.set('sessionId', sessionId)
    addAuth(token, sessionId)
  },
  catchSocketAction(state, data) {
    state.socketAction = data;
  },
}

const actions = {
  // 登录
  Login({
          commit,
          dispatch
        }, userInfo) {
    return new Promise((resolve, reject) => {
      loginAPI(userInfo).then(res => {
        console.info("Promise data:",res);
        const data = res.result ;
        console.info("Promise res.data:",data);
        let user = {};
        user.user_id= data.userId;
        user.mobile=data.mobile;
        user.portrait = data.portrait;
        user.mobile=data.mobile;
        console.info("user",user);
        commit('SET_AUTH', data)
        commit('SET_USERINFO', user)
        resolve(res)
      }).catch(error => {
        console.log(error);
         dispatch('LogOut')
         reject(error)
      })
    })
  },

  // 获取用户信息
  GetUserInfo({
                commit,
                state
              }) {
    return new Promise((resolve, reject) => {
      // adminUsersReadAPI().then(response => {
      //     // 邮件信息 走之前的逻辑
      //     commit('SET_USERINFO', response.data)
      //     resolve(response)
      // }).catch(error => {
      //     reject(error)
      // })
    })
  },

  // 登出
  LogOut({
           commit
         }) {
    return new Promise((resolve, reject) => {
      logoutAPI().then(() => {
        /** flush 清空localStorage .rm('authToken') 按照key清除 */
        Lockr.rm('authToken');
        Lockr.rm('sessionId');
        removeAuth()
        resetRouter()
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },
  // 获取权限
  getAuth({
            commit
          }) {
    return new Promise((resolve, reject) => {
      adminIndexAuthListAPI().then((response) => {
        const data = response.data
        Lockr.set('authList', data)
        data.wkFirstModel = data.firstModel
        commit('SET_ALLAUTH', data)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },
  sendSMS({commit, state}, param) {
    console.info("commit:", commit, " state:", state, " param:", param)
    return new Promise((resolve, reject) => {
      sendSMS(param).then(res => {
        const data = res.data || data
        console.info("sendSMS  ",data);
        resolve(res)
      }).catch(error => {
        console.log(error);
      })
    });
  },
}

export default new Vuex.Store({
  state,
  mutations,
  actions
})
